/* eslint-disable */
import {getDict as getDictApi} from "@/api/sys/dict";
import type {DictCache} from "@/model/cache/DictCache";
import {YesNo} from "@/model/common/SysEnums";
import storage from "@/config/storage";
import axios from "axios";
import {currJurisdiction} from "@/api/auth/auth";

function getDictDataMap(dict: DictCache) {
    let dataMap = new Map();
    for (let data of dict.datas) {
        dataMap.set(data.code, data)
    }
    return dataMap
}

export function getDict(dictCode: string): Promise<DictCache> {
    return new Promise((resolve, reject) => {
        if (!dictCode || dictCode == '') {
            reject(new Error('字典编码不能为空'))
            return
        }
        let cacheKey = "dict:" + dictCode
        let dictJson = storage.getItem(cacheKey)
        if (dictJson) {
            let dict = JSON.parse(dictJson)
            dict.dataMap = getDictDataMap(dict)
            resolve(dict)
        }
        getDictApi({dictCode: dictCode}).then(res => {
            let dict = res.data;
            storage.setItem(cacheKey, JSON.stringify(dict))
            dict.dataMap = getDictDataMap(dict)
            resolve(dict);
        })
    });
}

export function cleanDictCache() {
    for (let i = 0, len = storage.length; i < len; i++) {
        let key = storage.key(i);
        if (key && key.startsWith("dict:")) {
            storage.removeItem(key)
        }
    }
}

export function getEnableDict(dictCode: string) {
    return new Promise((resolve, reject) => {
        getDict(dictCode).then(res => {
            let dict = res
            let datas = [];
            for (const data of dict.datas) {
                if (data.status == YesNo.Yes) {
                    datas.push(data)
                }
            }
            dict.datas = datas;
            dict.dataMap = getDictDataMap(dict)
            resolve(dict)
        })
    });
}

export function getDictMap(...dictCodes: any) {
    return new Promise((resolve, reject) => {
        let promises = []
        for (let dictCode of dictCodes) {
            promises.push(getDict(dictCode))
        }
        Promise.all(promises).then(res => {
            let dictMap = new Map();
            for (const dict of res) {
                dictMap.set(dict.code, dict)
            }
            resolve(dictMap)
        })
    });
}

export function getDictDataName(dictCode: string, dictDataValue: any) {
    return new Promise((resolve, reject) => {
        getDict(dictCode).then(res => {
            let dictData = res.dataMap.get(dictDataValue)
            if (dictData) {
                resolve(dictData.name)
            }
        })
    })
}

export function getFileContent(filePath: string) {
    return axios.get(filePath, {
        responseRaw: true
    });
}

export function loadRolePermission() {
    currJurisdiction().then(res => {
        let data = res.data
        storage.setItem('roles', JSON.stringify(data.roleCodes))
        storage.setItem('permissions', JSON.stringify(data.permissionCodes))
    })
}

export function isRole(...roleCodes: any): boolean {
    let rolesJson = storage.getItem("roles");
    // @ts-ignore
    let roles: Set<String> = JSON.parse(rolesJson)
    for (let checkCode of roleCodes) {
        // @ts-ignore
        for (let userRole of roles) {
            if (checkCode == userRole) {
                return true;
            }
        }
    }
    return false
}

export function isPermission(...permissionCodes: any): boolean {
    let permissionsJson = storage.getItem("permissions");
    // @ts-ignore
    let permissions: Set<String> = JSON.parse(permissionsJson)
    for (let checkCode of permissionCodes) {
        // @ts-ignore
        for (let userPermission of permissions) {
            if (checkCode == userPermission) {
                return true;
            }
        }
    }
    return false
}

export function getFile(accept: string) {
    let input = document.createElement('input')
    input.setAttribute('type', 'file')
    if (accept) {
        input.setAttribute('accept', accept)
    }
    let res = new Promise((resolve, reject) => {
        input.addEventListener("input", (e) => {
            // @ts-ignore
            resolve(e.target.files[0])
        });
    })
    input.click()
    return res
}

